<style type="text/css">
    #step1, #step2, #step3, #step4{
        padding: 20px;
        padding-left: 260px;
        background-repeat: no-repeat;
        background-position-y: 50%;
        height:100%;
    }
    #step1{
        background-image: url('<?php echo $this->Html->url('../img/installation/Settings-icon.png'); ?>');
    }
    #step2{
        background-image: url('<?php echo $this->Html->url('../img/installation/Places-server-database-icon.png'); ?>');
    }
    #step3{
        background-image: url('<?php echo $this->Html->url('../img/installation/Actions-document-import-icon.png'); ?>');
    }
    #step4{
        background-image: url('<?php echo $this->Html->url('../img/installation/Folder-Live-Data-icon.png'); ?>');
    }
</style>
<script>
    $(function() {
        $('body').layout('collapse', 'west');
        $('#btnNext').click(function() {
            if($( "#btnNext" ).hasClass( "l-btn-disabled" )){
                return false;
            }
            if ($('#step1').is(":visible")) {
                $('#step1').slideUp();
                $('#step2').slideDown();
                $('#btnNext').linkbutton('enable');
                $('#btnBefore').linkbutton('enable');
            } else
            if ($('#step2').is(":visible")) {
                $('#step2').slideUp();
                $('#step3').slideDown();
                $('#btnNext').linkbutton('disable');
                $('#btnBefore').linkbutton('enable');
            } else
            if ($('#step3').is(":visible")) {
                $('#step3').slideUp();
                $('#step4').slideDown();
                $('#btnNext').linkbutton('disable');
                $('#btnBefore').linkbutton('enable');
            }
        });
        $('#btnBefore').click(function() {
            if($( "#btnBefore" ).hasClass( "l-btn-disabled" )){
                return false;
            }
            if ($('#step2').is(":visible")) {
                $('#step2').slideUp();
                $('#step1').slideDown();
                $('#btnNext').linkbutton('enable');
                $('#btnBefore').linkbutton('disable');
            } else
            if ($('#step3').is(":visible")) {
                $('#step3').slideUp();
                $('#step2').slideDown();
                $('#btnNext').linkbutton('enable');
                $('#btnBefore').linkbutton('enable');
            } else
            if ($('#step4').is(":visible")) {
                $('#step4').slideUp();
                $('#step3').slideDown();
                $('#btnNext').linkbutton('enable');
                $('#btnBefore').linkbutton('enable');
            }
        });
        $('#testCon').click(function() {
            testConnection();
        });
        $('#createTables').click(function() {
            createTables();
        });
        $('#createFile').click(function() {
            createDatabaseFile();
        });
        $('#step2').hide();
        $('#step3').hide();
        $('#step4').hide();
        $('#step4 .success').hide();

    });

    function resetDatabase() {
        $('div#divLoader').window('open');
        $('#resetDatabaseDiv').slideUp();
        $.ajax({
            type: "GET",
            url: '<?php echo $this->Html->url(array('controller' => 'CoreInstallation', 'action' => 'resetDatabaseAndFiles')); ?>',
            success: function(data) {
                $('#step4 .info').html($('#step4 .info').html()+'<pre>' + data + '</pre>');
                $('#resetDatabaseDiv').html('<pre>' + data + '</pre>');
                $('#resetDatabaseDiv').slideDown();
                $('div#divLoader').window('close');
            }
        });

    }
    function testConnection() {
        if ($('#DbConfigStep2Form').form('validate')) {
            $('div#divLoader').window('open');
            $.ajax({
                type: "POST",
                url: '<?php echo $this->Html->url(array('controller' => 'CoreInstallation', 'action' => 'testConnectionJson')); ?>',
                data: {nameDb: $('#DbConfigNameDb').val(), userDb: $('#DbConfigUserDb').val(), passDb: $('#DbConfigPassDb').val(), addressDb: $('#DbConfigAddressDb').val()},
                success: function(result) {
                    $('div#divLoader').window('close');
                    if (result == true || result == 'true') {
                        $.messager.alert('Connection', 'Success', 'info');
                        $('#step3').linkbutton('enable');
                    } else {
                        $.messager.alert('Connection', 'Error', 'error');
                        $('#step3').linkbutton('disable');
                    }
                }
            });
        }
    }
    function createTables() {
        $('div#divLoader').window('open');
        $.ajax({
            type: "POST",
            url: '<?php echo $this->Html->url(array('controller' => 'CoreInstallation', 'action' => 'useDump')); ?>',
            success: function(data) {
                $('#step3 .info').html(data);
                $('div#divLoader').window('close');
                $('#btnNext').linkbutton('enable');
            }
        });
    }
    function createDatabaseFile() {
        $('div#divLoader').window('open');
        $.ajax({
            type: "POST",
            url: '<?php echo $this->Html->url(array('controller' => 'CoreInstallation', 'action' => 'createDatabaseFile')); ?>',
            success: function(data) {
                $('#step4 .info').html(data);
                $('#step4 .success').slideDown();
                setTimeout(function (){
                    $('div#divLoader').window('close');
                    $.messager.confirm('Confirm', 'Do you want to reset the DataBase?', function(r) {
                        if (r) {
                            $.messager.confirm('Confirm', 'All your data will be replaced and there is no Rollback', function(r) {
                                if (r) {
                                    resetDatabase();
                                }
                            })
                        }
                    });
                }, 2000);
            }
        });
    }
</script>
<?php
$step1 = true;

if (!Configure::read('debug')):
    throw new NotFoundException();
endif;
App::uses('Debugger', 'Utility');
?>
<?php
if (Configure::read('debug') > 0):
    Debugger::checkSecurityKeys();
endif;
?>
<div id="cc" class="easyui-layout" data-options="fit:true" >
    <div data-options="region:'center'" style="padding:5px;">
        <div id="step1">
            <p id="url-rewriting-warning" style="background-color:#e32; color:#fff;">
                <?php echo __d('cake_dev', 'URL rewriting is not properly configured on your server.'); ?>
                1) <a target="_blank" href="http://book.cakephp.org/2.0/en/installation/advanced-installation.html#apache-and-mod-rewrite-and-htaccess" style="color:#fff;">Help me configure it</a>
                2) <a target="_blank" href="http://book.cakephp.org/2.0/en/development/configuration.html#cakephp-core-configuration" style="color:#fff;">I don't / can't use URL rewriting</a>
            </p>
            <p>
                <?php
                if (version_compare(PHP_VERSION, '5.2.8', '>=')):
                    echo '<span class="notice success">';
                    echo __d('cake_dev', 'Your version of PHP is 5.2.8 or higher.');
                    echo '</span>';
                else:
                    echo '<span class="notice">';
                    echo __d('cake_dev', 'Your version of PHP is too low. You need PHP 5.2.8 or higher to use CakePHP.');
                    echo '</span>';
                    $step1 = false;
                endif;
                ?>
            </p>
            <p>
                <?php
                if (is_writable(TMP)):
                    echo '<span class="notice success">';
                    echo __d('cake_dev', 'Your tmp directory is writable.');
                    echo '</span>';
                else:
                    echo '<span class="notice">';
                    echo __d('cake_dev', 'Your tmp directory is NOT writable.');
                    echo '</span>';
                    $step1 = false;
                endif;
                ?>
            </p>
            <p>
                <?php
                $settings = Cache::settings();
                if (!empty($settings)):
                    echo '<span class="notice success">';
                    echo __d('cake_dev', 'The %s is being used for core caching. To change the config edit APP/Config/core.php ', '<em>' . $settings['engine'] . 'Engine</em>');
                    echo '</span>';
                else:
                    echo '<span class="notice">';
                    echo __d('cake_dev', 'Your cache is NOT working. Please check the settings in APP/Config/core.php');
                    echo '</span>';
                    $step1 = false;
                endif;
                ?>
            </p>
            <?php
            App::uses('Validation', 'Utility');
            if (!Validation::alphaNumeric('cakephp')) {
                echo '<p><span class="notice">';
                echo __d('cake_dev', 'PCRE has not been compiled with Unicode support.');
                echo '<br/>';
                echo __d('cake_dev', 'Recompile PCRE with Unicode support by adding <code>--enable-unicode-properties</code> when configuring');
                echo '</span></p>';
                $step1 = false;
            }
            ?>

            <p>
                <?php
                if (CakePlugin::loaded('DebugKit')):
                    echo '<span class="notice success">';
                    echo __d('cake_dev', 'DebugKit plugin is present');
                    echo '</span>';
                else:
                    echo '<span class="notice">';
                    echo __d('cake_dev', 'DebugKit is not installed. It will help you inspect and debug different aspects of your application.');
                    echo '<br/>';
                    echo __d('cake_dev', 'You can install it from %s', $this->Html->link('github', 'https://github.com/cakephp/debug_kit'));
                    echo '</span>';
                    $step1 = false;
                endif;
                ?>
            </p>
        </div>
        <div id="step2">
            <?php echo $this->Form->create('DbConfig'); ?>
            <fieldset>
                <?php
                //echo $this->Form->input('mysqlBinPath', array('placeholder' => 'C:/Program Files (x86)/Zend/MySQL55/bin/', 'required' => true, "class" => "easyui-validatebox", "value" => "C:/Program Files (x86)/Zend/MySQL55/bin/"));
                echo $this->Form->input('addressDb', array('placeholder' => '127.0.0.1', 'required' => true, "class" => "easyui-validatebox"));
                echo $this->Form->input('userDb', array('placeholder' => 'root', 'required' => true, "class" => "easyui-validatebox"));
                echo $this->Form->input('passDb', array('type' => 'password'));
                echo $this->Form->input('nameDb', array('placeholder' => 'coreCake', 'required' => true, "class" => "easyui-validatebox"));
                ?>
            </fieldset>
            <?php echo $this->Form->end(); ?> 
            <div style="text-align: center;">
                <a id="testCon" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-arrow-circle-135-left'"><?php echo __('Test Connection'); ?></a>     
            </div>
        </div>
        <div id="step3">
            <div style="text-align: center;">
                <a id="createTables" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-tables-relation'"><?php echo __('Create Tables'); ?></a>     
            </div>
            <div class="info"></div>
        </div>
        <div id="step4">
            <div style="text-align: center;">
                <a id="createFile" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-blue-document-text'"><?php echo __('Create Database Config File'); ?></a>     
            </div>
            <div class="info"></div>
            <div class="success">
                If was not found any error, then the installation was successful.<br>
                <a href="<?php echo $this->Html->url(array('controller' => 'pages', 'action' => 'index')); ?>">click here</a> and go to the main system page</div>
        </div>
    </div>
    <div data-options="region:'south'" style="height:50px; padding: 5px;">
        <div style="text-align: center;">
            <a id="btnBefore" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-arrow-turn-180', disabled: true"><?php echo __('Before'); ?></a> 
            <a id="btnNext" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-arrow-turn'<?php
               if (!$step1) {
                   echo ", disabled: true";
               }
               ?>" ><?php echo __('Next'); ?></a>    
        </div>
    </div>
</div>